'''
    自动取款机存取款管理系统
    1.用户信息数据源
    2.查询余额信息
    3.取款操作
    4.存款操作
    5.用户登录
    6.退出系统
    7.界面和交互
'''

# 记录当前登录用户id
loginid=-1


# 1.用户账户数据源
account_list=[
    {'name':'zhangsan','balance':2000,'password':'123456'},
    {'name':'lisi','balance':1000,'password':'456789'},
    {'name':'wangwu','balance':300,'password':'654321'}]

# 2.查询余额信息
def show_account_info(uid):
    account_dict = account_list[uid]
    print('|{0:<5}|{1:<10}|{2:<10}|'.format('uid','name', 'balance'))
    print('|{0:<5}|{1:<10}|{2:<10}|'.format(str(uid+1), account_dict['name'], account_dict['balance']))

# 3. 取款操作
def Withdraw(uid,money):
    if account_list[uid]['balance'] >= money:
        b=account_list[uid]['balance']-money
        account_list[uid]['balance'] = b
        return True
    else:
        print('余额不足')
    return False

# 4.存款款操作
def deposit(uid,money):
    account_list[uid]['balance']+=money

# 5. 用户登录时check登录用户是否合法
def check_account(uid,password):
    global loginid
    account_num=len(account_list)
    if ((uid>account_num) or (uid<0)):
        loginid = -1
        print('账户id错误')
        return False
    account_dict = account_list[uid]
    if account_dict['password']!=password:
        print('登录密码错误')
        return False
    loginid = uid+1
    return True

# 6.退出
def loginOut():
    global loginid
    loginid=-1

# 7.交互界面
while True:
    # 输出登录界面
    print('=' * 12, '自动取款机存取款管理系统', '=' * 12)
    print('{:<1} {:<13} '.format(' ', '1. 登录'))
    print('=' * 40)
    key = input('请输入对应选择')
    if key=='1':
        uid=input('请输入id号')
        password=input('请输入密码')
        userid=int(uid)
        execute_result=check_account(userid-1,password)
        if execute_result==False:
            print('登录失败')
            input('按回车继续：')
        else:
            while True:
                print('=' * 12, '自动取款机存取款管理系统', '=' * 12)
                print('{:<1} {:<13} {:<13}'.format(' ', '1. 查看余额', '2. 取款操作'))
                print('{:<1} {:<13} {:<13}'.format(' ', '3. 存款操作', '4. 退出系统'))
                print('=' * 40)
                key = input('请输入对应选择')
                # 根据输入操作值，执行对应操作
                if key == '1':
                    print('=' * 12, '查询账户余额', '=' * 12)
                    password = input('请输入密码')
                    execute_result = check_account(loginid-1, password)
                    if execute_result==True:
                        show_account_info(loginid-1)
                    input('按回车继续：')
                if key == '2':
                    print('=' * 12, '取款操作', '=' * 12)
                    password = input('请输入密码')
                    execute_result = check_account(loginid-1, password)
                    if execute_result == True:
                        money=input('请输入取款金额')
                        execute_result=Withdraw(loginid-1, int(money))
                        if execute_result==True:
                            show_account_info(loginid-1)
                    input('按回车继续：')
                if key == '3':
                    print('=' * 12, '存款操作', '=' * 12)
                    password = input('请输入密码')
                    execute_result = check_account(loginid-1, password)
                    if execute_result == True:
                        money = input('请输入存款金额')
                        deposit(loginid-1, int(money))
                        show_account_info(loginid-1)
                    input('按回车继续：')
                elif key == '4':
                    loginOut()
                    print('=' * 12, '再见', '=' * 12)
                    break
                else:
                    print('=' * 12, '操作无效', '=' * 12)

    else:
        print('=' * 12, '操作无效', '=' * 12)
        input('按回车继续：')



